package com.rabbit.controller;

import com.github.pagehelper.PageInfo;
import com.rabbit.annotation.Log;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.OperType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.DictDataService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.Delete;
import com.rabbit.validate.base.Enable;
import com.rabbit.validate.base.Insert;
import com.rabbit.validate.base.Update;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.DictDataReq;
import com.rabbit.vo.res.DictDataRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <h1>字典键值管理</h1>
 *
 * @author zjw
 * @date 2022/02/20 17:31
 */
@RestController
@RequestMapping("/dictData")
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResultVO<Object> list(@RequestBody DictDataReq dictDataReq) {
        PageInfo<DictDataRes> pageInfo = dictDataService.queryDictDataList(dictDataReq);
        return ResultGenUtil.genSuccess(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除字典键值
     */
    @PostMapping("/delete")
    @Log(name = "字典键值管理", type = OperType.DELETE)
    public ResultVO<Object> deleteDictData(@Validated(Delete.class) @RequestBody DictDataReq dictDataReq) {
        dictDataService.deleteDictData(dictDataReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 调整字典键值状态
     */
    @PostMapping("/changeEnabled")
    @Log(name = "字典键值管理", type = OperType.UPDATE)
    public ResultVO<Object> changeEnabled(@LoginUser User loginUser, @Validated(Enable.class) @RequestBody DictDataReq dictDataReq) {
        dictDataReq.setOperatorUserName(loginUser.getUserName());
        dictDataService.updateDictDataEnabled(dictDataReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 查询指定字典键值
     */
    @GetMapping("/queryDictDataById")
    public ResultVO<Object> queryDictDataById(@RequestParam Long dataId) {
        return ResultGenUtil.genSuccess(dictDataService.queryDictDataById(dataId));
    }

    /**
     * 新增字典键值
     */
    @PostMapping("/add")
    @Log(name = "字典键值管理", type = OperType.INSERT)
    public ResultVO<Object> addDict(@LoginUser User loginUser, @Validated(Insert.class) @RequestBody DictDataReq dictDataReq) throws BusinessException {
        dictDataService.checkUnique(dictDataReq, 1);
        dictDataReq.setOperatorUserName(loginUser.getUserName());
        dictDataService.addDictData(dictDataReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 修改字典键值
     */
    @PostMapping("/update")
    @Log(name = "字典键值管理", type = OperType.UPDATE)
    public ResultVO<Object> updateDict(@LoginUser User loginUser, @Validated(Update.class) @RequestBody DictDataReq dictDataReq) throws BusinessException {
        dictDataService.checkUnique(dictDataReq, 2);
        dictDataReq.setOperatorUserName(loginUser.getUserName());
        dictDataService.updateDictData(dictDataReq);
        return ResultGenUtil.genSuccess();
    }
}
